home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / demo / rotplnnr.lha / RouteConv.c < prev    next >
C/C++ Source or Header  |  1994-08-23  |  12KB  |  468 lines

  1. /* RouteParse.c -- Converts RoadRoute files to RoutePlanner format */
  2. /* (C) 1994 Chris Lawrence */
  3.  
  4. char __stdiowin[]="CON:0/11//60/";
  5.  
  6. #include "RouteConv_rev.h"
  7.  
  8. #include <libraries/mui.h>
  9.  
  10. #include <proto/muimaster.h>
  11. #include <proto/dos.h>
  12. #include <proto/exec.h>
  13. #include <proto/icon.h>
  14. #include <proto/intuition.h>
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19.  
  20. #include <reg.h>
  21.  
  22. #include "RoutePlanner.h"
  23.  
  24. #define COPYRIGHT VERS" ("DATE") -- Copyright 1994 Chris Lawrence"
  25.  
  26. APTR app=NULL, mainwindow=NULL;
  27. struct DiskObject *disko=NULL;
  28.  
  29. int main(void);
  30. ULONG FindCity(struct List *citylist,char *cityname);
  31. BOOL Convert(char *cityname, char *roadname, char *outname, UWORD Units);
  32.  
  33. void ButtonBehave(APTR button, LONG notifyval);
  34.  
  35. void ButtonBehave(APTR button, LONG notifyval)
  36.   {
  37.   DoMethod((ULONG *)button,MUIM_Notify,MUIA_Pressed,FALSE,app,2,
  38.     MUIM_Application_ReturnID, notifyval);
  39.   }
  40.  
  41. void _STD_32767_killapp(void);
  42.  
  43. void _STD_32767_killapp(void)
  44. {
  45.   // SAS/C Autotermination function
  46.   
  47.   // This removes the application using MUI_DisposeObject. It is called
  48.   // when the program exits. Normal exits would also save the application
  49.   // object.
  50.   
  51.   if(disko) FreeDiskObject(disko);
  52.   if(app) MUI_DisposeObject(app);
  53. }
  54.  
  55. char *measurelabels[]={"Kilometers (km)","Miles (mi)",NULL};
  56.  
  57. #define QUIT    MUIV_Application_ReturnID_Quit
  58. #define OK    1
  59.  
  60. struct DataBaseInfo dbi;
  61.  
  62. int main(void)
  63. {
  64.   APTR BT_Ok, BT_Quit, ST_Cities, ST_Roads, ST_Output, CY_Distance;
  65.   APTR POP_Cities, POP_Roads, POP_Output;
  66.   ULONG i, sigs;
  67.   BOOL going=TRUE;
  68.   char *cities, *roads, *output;
  69.   
  70.   app=ApplicationObject,
  71.         MUIA_Application_Title,      "RouteConv",
  72.         MUIA_Application_Version,    "$VER: "VERS" ("DATE")",
  73.         MUIA_Application_Copyright,  "Copyright (C) 1994 Chris Lawrence",
  74.         MUIA_Application_Author,     "Chris Lawrence",
  75.         MUIA_Application_Description,"Conversion utility for RoutePlanner",
  76.         MUIA_Application_Base,       "ROUTECONV",
  77.         MUIA_Application_SingleTask, FALSE,
  78.         MUIA_Application_DiskObject,
  79.           disko=GetDiskObjectNew("PROGDIR:RouteConv"),
  80.         MUIA_Application_HelpFile,   "PROGDIR:RoutePlanner.guide",
  81.         MUIA_HelpNode,               "RouteConv",
  82.         // MUIA_Application_Commands, COMMANDS,
  83.         
  84.         SubWindow,
  85.         mainwindow=WindowObject,
  86.           MUIA_Window_Title,       "RouteConv",
  87.           MUIA_Window_ID,          'RCNV',
  88.           MUIA_Window_NoMenus,     TRUE,
  89.           MUIA_Window_ScreenTitle, COPYRIGHT,
  90.           
  91.           WindowContents,
  92.           VGroup,
  93.             Child, ColGroup(2),
  94.               Child, KeyLabel2("City file",'f'),
  95.               Child, POP_Cities=PopaslObject,
  96.                 MUIA_Popstring_String, ST_Cities=KeyString("Cities",256,'f'),
  97.                 MUIA_Popstring_Button, PopButton(MUII_PopFile),
  98.                 ASLFR_TitleText, "Choose city file...",
  99.                 End,
  100.               Child, KeyLabel2("Route file",'r'),
  101.               Child, POP_Roads=PopaslObject,
  102.                 MUIA_Popstring_String, ST_Roads=KeyString("Roads",256,'r'),
  103.                 MUIA_Popstring_Button, PopButton(MUII_PopFile),
  104.                 ASLFR_TitleText, "Choose route file...",
  105.                 End,
  106.               Child, KeyLabel2("Output file",'o'),
  107.               Child, POP_Output=PopaslObject,
  108.                 MUIA_Popstring_String, ST_Output=KeyString("USA.route",256,'o'),
  109.                 MUIA_Popstring_Button, PopButton(MUII_PopFile),
  110.                 ASLFR_TitleText, "Choose output file...",
  111.                 ASLFR_DoSaveMode, TRUE,
  112.                 End,
  113.               Child, KeyLabel1("Distances in",'d'),
  114.               Child, CY_Distance=KeyCycle(measurelabels,'d'),
  115.               End,
  116.             Child,HGroup,
  117.               MUIA_Group_SameSize, TRUE,
  118.               Child, BT_Ok=KeyButton("Convert",'c'),
  119.               Child, BT_Quit=KeyButton("Quit",'q'),
  120.               End,
  121.             End,
  122.           End,
  123.         End;
  124.   
  125.   if(app)
  126.     {
  127.     DoMethod(mainwindow,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,
  128.       app,2,MUIM_Application_ReturnID,QUIT);
  129.     
  130.     ButtonBehave(BT_Ok, OK);
  131.     ButtonBehave(BT_Quit, QUIT);
  132.     
  133.     DoMethod(mainwindow,MUIM_Window_SetCycleChain,ST_Cities,ST_Roads,
  134.       ST_Output,NULL);
  135.     
  136.     set(CY_Distance,MUIA_Cycle_Active,1);
  137.     
  138.     set(mainwindow,MUIA_Window_Open,TRUE);
  139.     get(mainwindow,MUIA_Window_Open,&i);
  140.     if(i)
  141.       {
  142.       while(going)
  143.         {
  144.         switch(DoMethod(app,MUIM_Application_Input,&sigs))
  145.           {
  146.           case 0:
  147.             break;
  148.           
  149.           case MUIV_Application_ReturnID_Quit:
  150.             get(POP_Cities,MUIA_Popasl_Active,&i);
  151.             if(!i) get(POP_Roads,MUIA_Popasl_Active,&i);
  152.             if(!i) get(POP_Output,MUIA_Popasl_Active,&i);
  153.             
  154.             if(i)
  155.               {
  156.               MUI_Request(app,mainwindow,0,NULL,"_Ok",
  157.                 "Can't quit while ASL popups are open.");
  158.               }
  159.             else
  160.               going=FALSE;
  161.             break;
  162.           
  163.           case OK:
  164.             get(ST_Cities,MUIA_String_Contents,&cities);
  165.             get(ST_Roads,MUIA_String_Contents,&roads);
  166.             get(ST_Output,MUIA_String_Contents,&output);
  167.             get(CY_Distance,MUIA_Cycle_Active,&i);
  168.             
  169.             set(app,MUIA_Application_Sleep,TRUE);
  170.             if(!Convert(cities,roads,output,(UWORD)i))
  171.               {
  172.               MUI_Request(app,mainwindow,0L,NULL,"_Ok",
  173.                 "An error occurred during conversion.");
  174.               }
  175.             set(app,MUIA_Application_Sleep,FALSE);
  176.             break;
  177.           }
  178.         
  179.         if(sigs && going) Wait(sigs);
  180.         }
  181.       }
  182.     else
  183.       MUI_Request(app,NULL,0,NULL,"Ok","Unable to create main window.");
  184.     }
  185.   else
  186.     MUI_Request(NULL,NULL,0,"RouteConv","Ok","Unable to create application.");
  187. }
  188.  
  189. BOOL Convert(char *cityname, char *roadname, char *outname, UWORD Units)
  190. {
  191.   FILE *incity, *inroad, *out;
  192.   struct List cities, roads;
  193.   volatile int citycount=0,roadcount=0;
  194.   int i, citylen, roadlen, outlen, curpos;
  195.   APTR statuswindow, BT_Cancel, GAUGE_Total, TX_Message;
  196.   BPTR filelock;
  197.   struct FileInfoBlock *fib;
  198.   
  199.   NewList(&cities);
  200.   NewList(&roads);
  201.   
  202.   strcpy(dbi.dbi_Author,VERS);
  203.   strcpy(dbi.dbi_EMail,DATE);
  204.   
  205.   fib=AllocDosObject(DOS_FIB,NULL);
  206.   if(!fib)
  207.     {
  208.     MUI_Request(app,mainwindow,0,NULL,"_Ok","Memory allocation failure.");
  209.     return FALSE;
  210.     }
  211.   
  212.   filelock=Lock(cityname, ACCESS_READ);
  213.   if(!filelock)
  214.     {
  215.     MUI_Request(app,mainwindow,0,NULL,"_Ok",
  216.       "Unable to open cities file\n"MUIX_PH"%s",cityname);
  217.     FreeDosObject(DOS_FIB,fib);
  218.     }
  219.   
  220.   Examine(filelock,fib);
  221.   citylen=fib->fib_Size;
  222.   UnLock(filelock);
  223.   
  224.   filelock=Lock(roadname, ACCESS_READ);
  225.   if(!filelock)
  226.     {
  227.     MUI_Request(app,mainwindow,0,NULL,"_Ok",
  228.       "Unable to open roads file\n"MUIX_PH"%s",roadname);
  229.     FreeDosObject(DOS_FIB,fib);
  230.     }
  231.   
  232.   Examine(filelock,fib);
  233.   roadlen=fib->fib_Size;
  234.   UnLock(filelock);
  235.   
  236.   FreeDosObject(DOS_FIB,fib);
  237.   
  238.   incity=fopen(cityname,"r");
  239.   if(!incity)
  240.     {
  241.     fprintf(stderr,"Error reading cities file %s\n",cityname);
  242.     return FALSE;
  243.     }
  244.   
  245.   while(!feof(incity))
  246.     {
  247.     static struct CityNode *cn;
  248.     static char instr[256], *ptr;
  249.     
  250.     cn=calloc(1,sizeof(struct CityNode));
  251.     if(!cn)
  252.       {
  253.       fprintf(stderr,"Can't allocate memory!\n");
  254.       fclose(incity);
  255.       return FALSE;
  256.       }
  257.     
  258.     fgets(instr,255,incity);
  259.     if(feof(incity)) continue;
  260.     ptr=strchr(instr,'\n');
  261.     if(ptr) *ptr='\0';
  262.     
  263.     ptr=strchr(instr,',');
  264.     if(ptr)
  265.       {
  266.       *ptr='\0';
  267.       strcpy(cn->cn_City.StateName,ptr+1);
  268.       }
  269.     strcpy(cn->cn_City.CityName,instr);
  270.     cn->cn_City.CityID=citycount;
  271.     cn->cn_City.Displayed=TRUE;
  272.     cn->cn_City.Intersection=FALSE;
  273.     cn->cn_City.CoordsExist=FALSE;
  274.     
  275.     AddTail(&cities,(struct Node *)cn);
  276.     citycount++;
  277.     
  278.     fprintf(stderr,"Converting cities... %4d\r",citycount);
  279.     }
  280.   
  281.   fclose(incity);
  282.   inroad=fopen(roadname,"r");
  283.   if(!inroad)
  284.     {
  285.     fprintf(stderr,"Error reading roads file %s\n",roadname);
  286.     return FALSE;
  287.     }
  288.   
  289.   fprintf(stderr,"\n");
  290.   while(!feof(inroad))
  291.     {
  292.     static char instr[256], *ptr, *inptr, *oldptr;
  293.     static char city1[50],city2[50],road[30];
  294.     static int distance,hours,minutes,citynum1,citynum2;
  295.     static struct RouteNode *rn;
  296.     
  297.     rn=calloc(1,sizeof(struct RouteNode));
  298.     if(!rn)
  299.       {
  300.       fprintf(stderr,"Can't allocate memory!\n");
  301.       fclose(inroad);
  302.       return FALSE;
  303.       }
  304.     
  305.     fgets(instr,255,inroad);
  306.     if(feof(inroad)) continue;
  307.     ptr=strchr(instr,'\n');
  308.     if(ptr) *ptr='\0';
  309.     
  310.     ptr=strchr(instr,',');
  311.     oldptr=instr;
  312.     if(ptr)
  313.       {
  314.       *ptr='\0';
  315.       inptr=ptr+1;
  316.       }
  317.     strcpy(city1,instr);
  318.     
  319.     ptr=strchr(inptr,',');
  320.     oldptr=inptr;
  321.     if(ptr)
  322.       {
  323.       *ptr='\0';
  324.       inptr=ptr+1;
  325.       }
  326.     strcpy(city2,oldptr);
  327.     
  328.     ptr=strchr(inptr,',');
  329.     oldptr=inptr;
  330.     if(ptr)
  331.       {
  332.       *ptr='\0';
  333.       inptr=ptr+1;
  334.       }
  335.     distance=atol(oldptr);
  336.     
  337.     ptr=strchr(inptr,':');
  338.     oldptr=inptr;
  339.     if(ptr)
  340.       {
  341.       *ptr='\0';
  342.       inptr=ptr+1;
  343.       }
  344.     hours=atol(oldptr);
  345.     
  346.     ptr=strchr(inptr,',');
  347.     oldptr=inptr;
  348.     if(ptr)
  349.       {
  350.       *ptr='\0';
  351.       inptr=ptr+1;
  352.       }
  353.     minutes=atol(oldptr);
  354.     strcpy(road,inptr);
  355.     
  356.     citynum1=FindCity(&cities,city1);
  357.     citynum2=FindCity(&cities,city2);
  358.     
  359.     rn->rn_Route.RouteID=roadcount;
  360.     rn->rn_Route.CityID[0]=citynum1;
  361.     rn->rn_Route.CityID[1]=citynum2;
  362.     rn->rn_Route.Distance=distance;
  363.     rn->rn_Route.Speed=hours*60+minutes;
  364.     rn->rn_Route.SpeedCode=USE_TIME;
  365.     rn->rn_Route.Units=Units;
  366.     strcpy(rn->rn_Route.Hiway,road);
  367.     
  368.     AddTail(&roads,(struct Node *)rn);
  369.     roadcount++;
  370.     
  371.     fprintf(stderr,"Converting roads... %5d\r",roadcount);
  372.     }
  373.   fclose(inroad);
  374.   
  375.   fprintf(stderr,"\n\n");
  376.   
  377.   out=fopen(outname,"wb");
  378.   if(!out)
  379.     {
  380.     fprintf(stderr,"Error creating file %s!\n",outname);
  381.     return FALSE;
  382.     }
  383.   
  384.   fprintf(out,"RPL2CITY");
  385.   
  386.   fwrite(&citycount,sizeof(citycount),1,out);
  387.   
  388.   i=1;
  389.   do {
  390.     static struct CityNode *cn;
  391.     
  392.     cn=(struct CityNode *)RemHead(&cities);
  393.     
  394.     //fprintf(infofile,"%4d %s, %s\n",cn->cn_City.CityID,cn->cn_City.CityName,
  395.     //  cn->cn_City.StateName);
  396.     
  397.     fwrite(&cn->cn_City,sizeof(struct CityData),1,out);
  398.     
  399.     fprintf(stderr,"Writing cities... %4d\r",i);
  400.     i++;
  401.     } while(!IsListEmpty(&cities));
  402.   
  403.   fprintf(stderr,"\n");
  404.   fprintf(out,"ROAD");
  405.   fwrite(&roadcount,sizeof(roadcount),1,out);
  406.   
  407.   i=1;
  408.   do {
  409.     static struct RouteNode *rn;
  410.     
  411.     rn=(struct RouteNode *)RemHead(&roads);
  412.     
  413.     //fprintf(infofile,"%s [from %d to %d]\n",rn->rn_Route.Hiway,
  414.     //  rn->rn_Route.CityID[0],rn->rn_Route.CityID[1]);
  415.     
  416.     fwrite(&rn->rn_Route,sizeof(struct RouteData),1,out);
  417.     
  418.     fprintf(stderr,"Writing roads... %5d\r",i);
  419.     i++;
  420.     } while(!IsListEmpty(&roads));
  421.   
  422.   dbi.dbi_AuthorCode=(ULONG)~0;
  423.   dbi.dbi_Security=DoFunkyThing(citycount+roadcount+dbi.dbi_AuthorCode+
  424.     StringSum(dbi.dbi_Author)+StringSum(dbi.dbi_EMail));
  425.   
  426.   fwrite(&dbi,sizeof(dbi),1,out);
  427.   
  428.   fprintf(stderr,"\n\nDone... %d cities and %d roads converted.\n",citycount,roadcount);
  429.   
  430.   fclose(out);
  431.   
  432.   return TRUE;
  433. }
  434.  
  435. ULONG FindCity(struct List *citylist,char *cityname)
  436. {
  437.   struct Node *n;
  438.   ULONG found=(ULONG)~0;
  439.   BOOL stateused=FALSE;
  440.   char city[60],state[30]="",*ptr;
  441.   
  442.   strcpy(city,cityname);
  443.   ptr=strchr(city,'/');
  444.   if(ptr)
  445.     {
  446.     *ptr='\0';
  447.     strcpy(state,ptr+1);
  448.     stateused=TRUE;
  449.     }
  450.   
  451.   for(n=citylist->lh_Head;n && found==(ULONG)~0;n=n->ln_Succ)
  452.     {
  453.     struct CityData *cd=&((struct CityNode *)n)->cn_City;
  454.     
  455.     if(stateused)
  456.       {
  457.       if(!stricmp(state,cd->StateName) && !stricmp(city,cd->CityName))
  458.         found=cd->CityID;
  459.       }
  460.     else
  461.       {
  462.       if(!stricmp(city,cd->CityName)) found=cd->CityID;
  463.       }
  464.     }
  465.   
  466.   return(found);
  467. }
  468.